/**
 ***********************************************************************************************************************
 * Copyright (c) 2020, China Mobile Communications Group Co.,Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * @file        tts_sample.c
 *
 * @brief       tts sample API for shell
 *
 * @revision
 * Date         Author          Notes
 * 2020-11-20   OneOS Team      First Version
 ***********************************************************************************************************************
 */

#include <oneos_config.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <os_dbg_ext.h>
#include "tts_url.h"
#include "tts_json.h"
#include "http_client.h"
#include "tts_audio.h"
#include "os_errno.h"
#include "os_stddef.h"

#ifdef TTS_AIVOICE_SAMPLE

static char *str_dup(char *s)
{
    char *url = NULL;
    int len = strlen(s) + 1;
    url = malloc(len);

    if (url == NULL)
    {
        return OS_NULL;
    }

    memcpy(url, s, len);

    return url;
}

static int tts_aivoice_player_func(int argc, char **argv)
{
    char *text = NULL;

    if (argc < 2)
    {

        os_kprintf("need more parameter.\n");
        return OS_ERROR;
    }
    else if (argc == 2)
    {
        text = str_dup(argv[1]);
        if (text == NULL)
        {
            os_kprintf("no memory for create text buffer.\n");
            return OS_ERROR;
        }
    }
    else
    {
        os_kprintf("Get only the first three parameters.\n");
        return OS_ERROR;
    }

    tts_player(text, 1);

    if (text != NULL)
    {
        free(text);
    }

    return OS_EOK;
}

static int tts_aivoice_per_func(int argc, char **argv)
{
    char *per = NULL;
    int per_int = 0;

    if (argc < 2)
    {

        os_kprintf("need more parameter.\n");
        return OS_ERROR;
    }
    else if (argc == 2)
    {
        per = str_dup(argv[1]);
        if (per == NULL)
        {
            os_kprintf("no memory for create text buffer.\n");
            return OS_ERROR;
        }
        per_int = atoi(per);
    }
    else
    {
        os_kprintf("Get only the first three parameters.\n");
        return OS_ERROR;
    }

    os_kprintf("per_int:%d\n", per_int);

    tts_aivoice_per(per_int);

    if (per != NULL)
    {
        free(per);
    }

    return OS_EOK;
}

static int tts_aivoice_init_func(int argc, char **argv)
{
    char *api_key = NULL;
    char *secret_key = NULL;

    if (argc < 3)
    {

        os_kprintf("need more parameter.\n");
        return OS_ERROR;
    }
    else if (argc == 3)
    {
        api_key = str_dup(argv[1]);
        if (api_key == NULL)
        {
            os_kprintf("no memory for create api_key buffer.\n");
            return OS_ERROR;
        }
        secret_key = str_dup(argv[2]);
        if (secret_key == NULL)
        {
            os_kprintf("no memory for create secret_key buffer.\n");
            return OS_ERROR;
        }
    }
    else
    {
        os_kprintf("Get only the first three parameters.\n");
        return OS_ERROR;
    }

    tts_aivoice_config(api_key, secret_key);

    if (api_key != NULL)
    {
        free(api_key);
    }
    if (secret_key != NULL)
    {
        free(secret_key);
    }

    return OS_EOK;
}

#ifdef OS_USING_SHELL
#include <shell.h>
SH_CMD_EXPORT(tts_aivoice_player, tts_aivoice_player_func, "Start player tts aisound, the argument is: Chinese text");
SH_CMD_EXPORT(tts_aivoice_per, tts_aivoice_per_func, "Set tts aisound pronunciation person, the argument is:num:0~4");
SH_CMD_EXPORT(tts_aivoice_init, tts_aivoice_init_func, "Init tts aisound, the argument is: api_key and secret_key");
#endif
#endif
